package cn.lg.soar.core.util.easyexcel.translate;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

/**
 * 翻译转换器
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class TranslateConverter implements Converter<Object> {

    @Override
    public Class<Object> supportJavaTypeKey() {
        return Object.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Object convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        InnerTranslateConverter<?> converter = Utils.getConverter(contentProperty.getField());
        if (converter == null) {
            throw new RuntimeException("未找到翻译注解");
        }
        return converter.toJava(cellData.getStringValue());
    }

    @Override
    public WriteCellData<?> convertToExcelData(Object o, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        InnerTranslateConverter<?> converter = Utils.getConverter(excelContentProperty.getField());
        if (converter == null) {
            throw new RuntimeException("未找到翻译注解");
        }
        return new WriteCellData<>(converter.toExcel(o));
    }

}
